/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import model.entity.Bill;
import model.entity.Comment;

/**
 *
 * @author KAKAIT
 */
public class CommentImp {
    
    public boolean addComment(Comment cmt)
    {
        boolean kq = true;
        try {
            PreparedStatement ps = ConnectDatabase.getConnection().prepareStatement("insert into comment values (?,?,?,?)");
            ps.setString(1, cmt.getUsername());
            ps.setString(2, cmt.getCmtcontent());
            ps.setString(3, cmt.getCmtdate());
            ps.setInt(4, cmt.getProductid());
            kq = ps.executeUpdate()>0;
        } catch (Exception e) {
            kq = false;
        }
        return kq;
    }
    
    public List<Comment> Listbillbyusername(int productid)
    {
        List<Comment> lcmt = new ArrayList<Comment>();
        try {
            
            Connection con = ConnectDatabase.getConnection();
            PreparedStatement ps = con.prepareStatement("select * from comment where productid = ?");
            ps.setInt(1, productid);
            ResultSet rs = ps.executeQuery();
            while(rs.next())
            {
                lcmt.add(new Comment(rs.getString("username"), rs.getString("cmtcontent"), rs.getString("cmtdate"), productid));
            }
        } catch (Exception e) {
            lcmt = null;
        }
       return lcmt;
    }
    
    
}
